package com.kang.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.kang.pojo.IntemInformation;
import com.kang.service.ItemsStorageStatisticsService;
import com.kang.until.SystemStatisticsPDF;
import com.kang.until.WarningPDF;
import com.vonechina.entity.page.InventoryWarningPage;
import com.vonechina.entity.page.ItemsStorageStatisticsPage;

/**
 * 物品入库统计
 * 
 * @author ysq
 *
 */
@Controller
@RequestMapping("statistics")
@SessionAttributes({ "itemsStorageStatisticsPage" })
public class StatisticsContrller extends com.kang.controller.Controller {
	/**
	 * 物品入库统计
	 */
	@Resource
	private ItemsStorageStatisticsService itemsStorageStatisticsService;

	/**
	 * 物品退货统计表
	 * 
	 * @param session
	 * @param model
	 * @param itemsStorageStatisticsPage
	 * @return
	 */
	@RequestMapping("returnorderSelectByWhere")
	public String returnorderSelectByWhere(HttpSession session, Model model,
			ItemsStorageStatisticsPage itemsStorageStatisticsPage) {
		initRight(session);
		if (checkRight("退货统计") != SUCCESS) {
			return "basic/noRight";
		}
		// 查询物品领用统计表
		List<Map<String, String>> iss = itemsStorageStatisticsService
				.returnorderSelectByWhere(itemsStorageStatisticsPage);
		model.addAttribute("iss", iss);

		itemsStorageStatisticsPage
				.setPageUrl("statistics/returnorderSelectByWhere.do");
		model.addAttribute("page", itemsStorageStatisticsPage);
		return "systemStatistics/returnorderStatistics/returnorderStatistics";
	}

	/**
	 * 物品领用统计表
	 * 
	 * @param session
	 * @param model
	 * @param itemsStorageStatisticsPage
	 * @return
	 */
	@RequestMapping("pickingSelectByWhere")
	public String pickingSelectByWhere(HttpSession session, Model model,
			ItemsStorageStatisticsPage itemsStorageStatisticsPage) {
		initRight(session);
		if (checkRight("领用统计") != SUCCESS) {
			return "basic/noRight";
		}
		// 查询物品领用统计表
		List<Map<String, String>> iss = itemsStorageStatisticsService
				.pickingSelectByWhere(itemsStorageStatisticsPage);
		model.addAttribute("iss", iss);

		itemsStorageStatisticsPage
				.setPageUrl("statistics/pickingSelectByWhere.do");
		model.addAttribute("page", itemsStorageStatisticsPage);
		return "systemStatistics/pickingStatistics/pickingStatistics";
	}

	/**
	 * 查询物品采购集合
	 * 
	 * @param session
	 * @param model
	 * @param itemsStorageStatisticsPage
	 * @return
	 */
	@RequestMapping("apsselectByWhere")
	public String apsselectByWhere(HttpSession session, Model model,
			ItemsStorageStatisticsPage itemsStorageStatisticsPage) {
		initRight(session);
		if (checkRight("采购统计") != SUCCESS) {
			return "basic/noRight";
		}
		// 查询普通入库统计
		List<Map<String, String>> iss = itemsStorageStatisticsService
				.apsselectByWhere(itemsStorageStatisticsPage);
		model.addAttribute("iss", iss);
		itemsStorageStatisticsPage.setPageUrl("statistics/apsselectByWhere.do");
		model.addAttribute("page", itemsStorageStatisticsPage);
		return "systemStatistics/articlePurchasingStatistics/articlePurchasingStatistics";
	}

	/**
	 * 查询物品入库集合
	 * 
	 * @param session
	 * @param model
	 * @param itemsStorageStatisticsPage
	 * @return
	 */
	@RequestMapping("selectByWhere")
	public String itemsStorageStatisticsSelectByWhere(HttpSession session,
			Model model, ItemsStorageStatisticsPage itemsStorageStatisticsPage) {
		initRight(session);
		if (checkRight("入库统计") != SUCCESS) {
			return "basic/noRight";
		}
		// 入库类型，0普通入库，1采购入库
		if (itemsStorageStatisticsPage.getItemsStorageStatisticsStatus() == 0) {
			// 查询普通入库统计
			List<Map<String, String>> iss = itemsStorageStatisticsService
					.selectGeneralstorageList(itemsStorageStatisticsPage);
			model.addAttribute("iss", iss);
		} else if (itemsStorageStatisticsPage.getItemsStorageStatisticsStatus() == 1) {
			// 查询采购入库统计
			List<Map<String, String>> iss2 = itemsStorageStatisticsService
					.selectWarehouseorderList(itemsStorageStatisticsPage);
			model.addAttribute("iss2", iss2);
		}
		itemsStorageStatisticsPage.setPageUrl("statistics/selectByWhere.do");
		model.addAttribute("page", itemsStorageStatisticsPage);
		return "systemStatistics/itemsStorageStatistics/itemsStorageStatistics";
	}
	
	
	/**
	 * 导出采购统计PDF文件
	 * 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("downloadPurchasingStatistics")
	public void downloadPurchasingStatisticsPDF(HttpServletResponse response, ItemsStorageStatisticsPage itemsStorageStatisticsPage)
			throws Exception {
		List<Map<String, String>> iss = itemsStorageStatisticsService
				.apsselectByWhere(itemsStorageStatisticsPage);
		//此方法为直接下载文件
		File file = SystemStatisticsPDF.PurchasingStatisticsPDF(iss);
		if (file.isFile() && file.exists()) {
			try {
				 InputStream fis = new BufferedInputStream(new FileInputStream(file));
				    byte[] buffer = new byte[fis.available()];
				    fis.read(buffer);
				    fis.close();
				    response.reset();
				    //乱码处理
				    //先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
				    response.addHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
				    response.addHeader("Content-Length", "" + file.length());
				    OutputStream os = new BufferedOutputStream(response.getOutputStream());
				    response.setContentType("application/octet-stream");
				    os.write(buffer);//输出文件
				    os.flush();
				    os.close();//关闭流，释放资源！
			} catch (Exception e) {
				e.printStackTrace();
			}
			file.delete();
		}
	}
	
	
	
}
